package city.spring.modules.medicalstation.drugwarehouseShelfExt.service.impl;

import city.spring.modules.medicalstation.drug.repository.DrugRepository;
import city.spring.modules.medicalstation.drugwarehouse.service.DrugwarehouseService;
import city.spring.modules.medicalstation.drugwarehouseShelfExt.entity.DrugwarehouseShelfExtEntity;
import city.spring.modules.medicalstation.drugwarehouseShelfExt.repository.DrugwarehouseShelfExtRepository;
import city.spring.modules.medicalstation.drugwarehouseShelfExt.service.DrugwarehouseShelfExtService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：医护站药品入库与货架关联表
 *
 * @author 谢金婷
 * @date 2020-09-24 16:34:05
 */
@CacheConfig(cacheNames = {DrugwarehouseShelfExtServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class DrugwarehouseShelfExtServiceImpl extends ServiceImpl<DrugwarehouseShelfExtRepository, DrugwarehouseShelfExtEntity> implements DrugwarehouseShelfExtService {
    private final DrugwarehouseService medicinewarehouseService;
    private final DrugRepository medicalstationMedicineRepository;

    public DrugwarehouseShelfExtServiceImpl(DrugwarehouseService medicinewarehouseService, DrugRepository medicalstationMedicineRepository) {
        this.medicinewarehouseService = medicinewarehouseService;
        this.medicalstationMedicineRepository = medicalstationMedicineRepository;
    }

    @Override
    public void saveDrugwarehouseShelfExt(DrugwarehouseShelfExtEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateDrugwarehouseShelfExt(DrugwarehouseShelfExtEntity entity) {
//        DrugwarehouseShelfExtEntity medicinewarehouseShelfExtEntity = getById(entity.getId());
//        BigDecimal beforeQuantity = medicinewarehouseShelfExtEntity.getWarehousingquantity() == null ? BigDecimal.ZERO : medicinewarehouseShelfExtEntity.getWarehousingquantity();
//
//        System.out.println("修改前入库数量"+beforeQuantity);

        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
//        MedicalstationMedicinewarehouseEntity medicinewarehousingEntity = medicinewarehouseService.getById(entity.getMedicinelwarehousingId());
//        entity.setTotal(entity.getWarehousingquantity().multiply(medicinewarehousingEntity.getUnitprice()));

        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
//        if (!updateById(entity)) {
//            throw new RuntimeException("修改信息失败");
//        }
//        BigDecimal afterQuantity = entity.getWarehousingquantity();
//        System.out.println("修改后入库数量"+entity.getWarehousingquantity());
//
//        MedicalstationMedicineEntity medicalstationMedicineEntity = medicalstationMedicineRepository.medicineByShelfIdAndCode(medicinewarehouseShelfExtEntity.getShelflevelId(), medicinewarehousingEntity.getMedicineCode());
//        System.out.println("入库货架数量修改，根据货架id和code获取药品信息"+medicalstationMedicineEntity);
//
//        // 原来的入库数量
//        BigDecimal warehousingquantity = medicalstationMedicineEntity.getWarehousingquantity() == null ? BigDecimal.ZERO : medicalstationMedicineEntity.getWarehousingquantity();
//        //原来的库存
//        BigDecimal quantity = medicalstationMedicineEntity.getQuantity() == null ? BigDecimal.ZERO : medicalstationMedicineEntity.getQuantity();
//        // 修改该药品入库数量和库存数量 ：减去修改前的数量+修改后的数量
//        medicalstationMedicineEntity.setWarehousingquantity(warehousingquantity.subtract(beforeQuantity).add(afterQuantity));
//        medicalstationMedicineEntity.setQuantity(quantity.subtract(beforeQuantity).add(afterQuantity));
//        medicalstationMedicineRepository.updateById(medicalstationMedicineEntity);


    }

    @Override
    public void deleteDrugwarehouseShelfExt(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteDrugwarehouseShelfExt(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }
}